from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton

class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setWindowTitle("点击创建新按钮示例")
        self.setGeometry(100, 100, 300, 200)

        layout = QVBoxLayout()

        button1 = QPushButton("按钮1")
        layout.addWidget(button1)

        def createButton():
            new_button = QPushButton("新按钮")
            layout.insertWidget(layout.count() - 1, new_button)  # 将新按钮插入到倒数第二个位置

        button2 = QPushButton("创建按钮")
        button2.clicked.connect(createButton)
        layout.addWidget(button2)

        container = QWidget()
        container.setLayout(layout)

        self.setCentralWidget(container)

if __name__ == "__main__":
    app = QApplication([])
    window = MyWindow()
    window.show()
    app.exec()